<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addCate"
          >添加分类</el-button
        >
      </div>
      <!-- 分类数据的表格 -->
      <el-table style="width: 100%" :data="cateList" border stripe>
        <el-table-column
          label="序号"
          type="index"
          width="100"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <!-- element的scope会传的数据中有一个row，里面带有每一行的数据id，name等 -->
          <template v-slot="scope">
            <el-button
              type="primary"
              size="mini"
              @click="updataCateBtnFn(scope.row)"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="removeCate(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加弹出框 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogFormVisible"
        width="35%"
        @closed="onAddClosedFn"
      >
        <!-- 添加的表单 -->
        <el-form
          :model="addForm"
          :rules="addRules"
          ref="addRef"
          label-width="80px"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input
              v-model="addForm.cate_name"
              minlength="1"
              maxlength="10"
            ></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input
              v-model="addForm.cate_alias"
              minlength="1"
              maxlength="15"
            ></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogqueding">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 修改弹出框 -->
    </el-card>
  </div>
</template>

<script>
import {
  getArtCateApi,
  addArtCateApi,
  amendAtrCateApi,
  removeAtrCateApi
} from '@/api/index'
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [], // 文章的分类列表
      dialogFormVisible: false, // 对话框的弹出与隐藏
      addForm: {
        // 表单数据
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        // 表单的验证规则
        // 添加表单的验证规则对象
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      },
      isEdit: false, // true为修改，false为新增加
      editId: '' // 保证正要编辑的数据的id值
    }
  },
  methods: {
    // 获取文章分类列表
    async getArtCateList () {
      const { data: res } = await getArtCateApi()
      this.cateList = res.data
    },
    // 添加分类的按钮函数
    addCate () {
      this.dialogFormVisible = true
      this.isEdit = false
      this.editId = ''
    },
    // 对话框的确定按钮,添加分类并验证表单
    dialogqueding () {
      // 表单验证
      this.$refs.addRef.validate(async (valid) => {
        // 表单验证成功
        if (valid) {
          // 添加分类状态
          if (!this.isEdit) {
            const { data: res } = await addArtCateApi(this.addForm)
            if (res.code === 0) {
              this.$message.success(res.message)
              this.getArtCateList()
            } else {
              return this.$message.error(res.message)
            }
            // 新增分类状态
          } else {
            this.addForm.id = this.editId
            console.log(this.addForm) // 把要编辑的文章分类id添加到对象上
            const { data: res } = await amendAtrCateApi(this.addForm)
            if (res.code !== 0) {
              return this.$message.error(res.message)
            } else {
              this.$message.success(res.message)
              this.getArtCateList()
            }
          }
          // 表单验证失败
        } else {
          return false
        }
      })
      this.dialogFormVisible = false
    },
    // 所以事件来关闭对话框时清空表单数据
    onAddClosedFn () {
      // 清空表单的数据变化*最初值*
      this.$refs.addRef.resetFields()
    },
    // 点击修改文章分类
    // value1=scope.row
    // 小bug：如果先点击*修改*在点击*添加*那么表单的值还是没有被重置，
    // 保留修改的名字和昵称，
    // 原因应为表单的创建的是异步的所以
    // this.addForm.cate_name = value1.cate_name
    // this.addForm.cate_alias = value1.cate_alias
    // 先执行，把初始值变成了表单的值，又因为element的重置数函数是
    // 清空表单的数据并变化*初始值*
    // this.$refs.addRef.resetFields()
    // 解决：
    updataCateBtnFn (value1) {
      this.dialogFormVisible = true
      this.isEdit = true // 修改状态
      this.editId = value1.id
      this.$nextTick(() => {
        this.addForm.cate_name = value1.cate_name
        this.addForm.cate_alias = value1.cate_alias
      })
    },
    // 删除分类
    async removeCate (value) {
      const { data: res } = await removeAtrCateApi(value.id)
      if (res.code !== 0) {
        return this.$message.error(res.message)
      } else {
        this.$message.success(res.message)
        this.getArtCateList()
      }
    }
  },
  // 获取文章分类列表
  async created () {
    this.getArtCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.addCate-box {
  width: 400px;
}
</style>
